//
//  RHMePlantInfoAlarmCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/6.
//

#import "RHMePlantInfoAlarmCell.h"
#import "RHMePlantDetailModel.h"

@interface RHMePlantInfoAlarmCell()
@property (nonatomic, strong) QMUILabel *todayAlarmLab;

@property (nonatomic, strong) QMUILabel *totalAlarmLab;

@property (nonatomic, strong) QMUILabel *todayFaultLab;

@property (nonatomic, strong) QMUILabel *totalFaultLab;

@end
@implementation RHMePlantInfoAlarmCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setDetailModel:(RHMePlantDetailModel *)detailModel
{
    _detailModel = detailModel;
    self.todayAlarmLab.text = [NSDecimalNumber decimalStringWithDoubleString:detailModel.todayAlarm nullStr:@"--"];
    self.totalAlarmLab.text = [NSDecimalNumber decimalStringWithDoubleString:detailModel.accumulateAlarm nullStr:@"--"];
    self.todayFaultLab.text = [NSDecimalNumber decimalStringWithDoubleString:detailModel.todayFault nullStr:@"--"];
    self.totalFaultLab.text = [NSDecimalNumber decimalStringWithDoubleString:detailModel.accumulateFault nullStr:@"--"];
    [self.todayAlarmLab sizeToFit];
    [self.totalAlarmLab sizeToFit];
    [self.todayFaultLab sizeToFit];
    [self.totalFaultLab sizeToFit];
    [self.todayAlarmLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(self.todayAlarmLab.width);
    }];
    [self.totalAlarmLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(self.totalAlarmLab.width);
    }];
    [self.todayFaultLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(self.todayFaultLab.width);
    }];
    [self.totalFaultLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(self.totalFaultLab.width);
    }];
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.qmui_borderColor = kBorderColor;
    bgView.qmui_borderWidth = 1;
    bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionRight|QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    //左上角标题
    QMUILabel *titleLab = [QMUILabel labelWithText:@"--" font:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443")];
    [bgView addSubview:titleLab];
    titleLab.text = @"故障告警监控".rh_localized;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
    }];
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(titleLab.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
    NSArray *titles = @[@"今日告警:".rh_localized,@"累计告警:".rh_localized,@"今日故障:".rh_localized,@"累计故障:".rh_localized];
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 55;
    CGFloat itemWidth = bgWidth / 2;
    //左边第一个
    __block UIView *lastView;
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = kView;
        itemView.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
        itemView.layer.cornerRadius = 8;
        itemView.layer.masksToBounds = YES;
        [itemBgView addSubview:itemView];
        QMUILabel *titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
        titleLab.text = title;
        titleLab.numberOfLines = 0;
        [itemView addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.centerY.mas_offset(0);
           
        }];
        
        QMUILabel *contentLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentRight];
        contentLab.text = @"--";
        contentLab.numberOfLines = 2;
        contentLab.tag = 100+idx;
        [itemView addSubview:contentLab];
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab.mas_right);
            make.right.mas_offset(-15);
            make.centerY.mas_offset(0);
        }];
        
        if (lastView) {
            if (idx%2==0) {
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(10);
                    make.size.equalTo(lastView);
                    make.top.equalTo(lastView.mas_bottom).offset(5);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right).offset(5);
                    make.size.top.equalTo(lastView);
                }];
            }
            
        }else{
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_offset(10);
                make.width.offset(itemWidth);
                make.height.offset(44);
            }];
        }
        
        if (idx == 0) {
            self.todayAlarmLab = contentLab;
        }
        if (idx == 1) {
            self.totalAlarmLab = contentLab;
        }
        if (idx == 2) {
            self.todayFaultLab = contentLab;
        }
        if (idx == 3) {
            self.totalFaultLab = contentLab;
        }
        lastView = itemView;
    }];
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-5);
    }];
    
    
}



@end
